/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package alwv.reports.pdf;

import alwv.database.DerbyConnection;
import alwv.database.Query;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.awt.Desktop;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Brad
 */
public class TaxContributionMember {

    private static final float LEFT = 0.25f;
    private static final float RIGHT = 0.25f;
    private static final float TOP = 50.0f;
    private static final float BOTTOM = 50.0f;

    public static void generateReport(Connection conn, int year, int memberID, String memberName, String filename) {

        ArrayList<Object> information = Query.getMemberDeductibleTransactions(conn, memberID, year);

        try {
            Document d = new Document(PageSize.A4);
            d.setMargins(LEFT, RIGHT, TOP, BOTTOM);

            PdfWriter writer = PdfWriter.getInstance(d, new FileOutputStream(filename));
            d.open();

            Font topFont = new Font(Font.FontFamily.TIMES_ROMAN, 14, Font.BOLD);
            Paragraph p = new Paragraph("Tax Contribution Report " + year + " for: " + memberName, topFont);
            p.setSpacingAfter(20f);
            p.setAlignment(Element.ALIGN_CENTER);
            d.add(p);

            PdfPTable table = new PdfPTable(3);
            float[] widths = {20f, 20f, 50f};
            table.setWidths(widths);
            table.setWidthPercentage(50f);

            table.addCell(headingCell("Date"));
            table.addCell(headingCell("Amount"));
            table.addCell(headingCell("Description"));


            BigDecimal total = new BigDecimal(0.0);
            total = total.setScale(2, BigDecimal.ROUND_HALF_EVEN);
            for (int i = 0; i < information.size(); i += 3) {
                String date = information.get(i).toString();
                String amount = String.valueOf(information.get(i + 1));
                String description = (String) information.get(i + 2);
                table.addCell(dateCell(date));
                table.addCell(amountCell(amount));
                table.addCell(descriptionCell(description));

                BigDecimal temp = new BigDecimal(((Double) information.get(i + 1)).doubleValue());
                total = total.add(temp);
            }

            d.add(table);

            total = total.setScale(2, BigDecimal.ROUND_HALF_EVEN);
            Font bottomFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
            p = new Paragraph("Total Contribution: " + total, bottomFont);
            p.setAlignment(Element.ALIGN_CENTER);
            p.setSpacingBefore(15f);
            d.add(p);

            d.close();

            File file = new File(filename);
            Desktop.getDesktop().open(file);
        } catch (IOException ex) {
            Logger.getLogger(MonthlyReport.class.getName()).log(Level.SEVERE, null, ex);
        } catch (DocumentException ex) {
            Logger.getLogger(MonthlyReport.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void generateForAllNonMembers(Connection conn, int year, String filename) {
        try {
            Document d = new Document(PageSize.A4);
            d.setMargins(LEFT, RIGHT, TOP, BOTTOM);

            PdfWriter writer = PdfWriter.getInstance(d, new FileOutputStream(filename));
            d.open();

            ArrayList<Integer> ids = Query.getAllNonMemberIDs(conn);

            for (int t = 0; t < ids.size(); t++) {
                int memberID = ids.get(t);
                String memberName = Query.getNonMemberName(conn, memberID);
                ArrayList<Object> information = Query.getMemberDeductibleTransactions(conn, memberID, year);
                if(information.size() > 0){
                    try {
                        d.newPage();
                        Font topFont = new Font(Font.FontFamily.TIMES_ROMAN, 14, Font.BOLD);
                        Paragraph p = new Paragraph("Tax Contribution Report " + year + " for: " + memberName, topFont);
                        p.setSpacingAfter(20f);
                        p.setAlignment(Element.ALIGN_CENTER);
                        d.add(p);

                        PdfPTable table = new PdfPTable(3);
                        float[] widths = {20f, 20f, 50f};
                        table.setWidths(widths);
                        table.setWidthPercentage(50f);

                        table.addCell(headingCell("Date"));
                        table.addCell(headingCell("Amount"));
                        table.addCell(headingCell("Description"));


                        BigDecimal total = new BigDecimal(0.0);
                        total = total.setScale(2, BigDecimal.ROUND_HALF_EVEN);
                        for (int i = 0; i < information.size(); i += 3) {
                            String date = information.get(i).toString();
                            String amount = String.valueOf(information.get(i + 1));
                            String description = (String) information.get(i + 2);
                            table.addCell(dateCell(date));
                            table.addCell(amountCell(amount));
                            table.addCell(descriptionCell(description));

                            BigDecimal temp = new BigDecimal(((Double) information.get(i + 1)).doubleValue());
                            total = total.add(temp);
                        }

                        d.add(table);

                        total = total.setScale(2, BigDecimal.ROUND_HALF_EVEN);
                        Font bottomFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
                        p = new Paragraph("Total Contribution: " + total, bottomFont);
                        p.setAlignment(Element.ALIGN_CENTER);
                        p.setSpacingBefore(15f);
                        d.add(p);


                    } catch (DocumentException ex) {
                        Logger.getLogger(MonthlyReport.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
            
            d.close();

            File file = new File(filename);
            Desktop.getDesktop().open(file);
        } catch (DocumentException ex) {
            Logger.getLogger(TaxContributionMember.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(MonthlyReport.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static void generateForAllMembers(Connection conn, int year, String filename) {
        try {
            Document d = new Document(PageSize.A4);
            d.setMargins(LEFT, RIGHT, TOP, BOTTOM);

            PdfWriter writer = PdfWriter.getInstance(d, new FileOutputStream(filename));
            d.open();

            ArrayList<Integer> ids = Query.getAllMemberIDs(conn);

            for (int t = 0; t < ids.size(); t++) {
                int memberID = ids.get(t);
                String memberName = Query.getMemberName(conn, memberID);
                ArrayList<Object> information = Query.getMemberDeductibleTransactions(conn, memberID, year);
                if(information.size() > 0){
                    try {
                        d.newPage();
                        Font topFont = new Font(Font.FontFamily.TIMES_ROMAN, 14, Font.BOLD);
                        Paragraph p = new Paragraph("Tax Contribution Report " + year + " for: " + memberName, topFont);
                        p.setSpacingAfter(20f);
                        p.setAlignment(Element.ALIGN_CENTER);
                        d.add(p);

                        PdfPTable table = new PdfPTable(3);
                        float[] widths = {20f, 20f, 50f};
                        table.setWidths(widths);
                        table.setWidthPercentage(50f);

                        table.addCell(headingCell("Date"));
                        table.addCell(headingCell("Amount"));
                        table.addCell(headingCell("Description"));


                        BigDecimal total = new BigDecimal(0.0);
                        total = total.setScale(2, BigDecimal.ROUND_HALF_EVEN);
                        for (int i = 0; i < information.size(); i += 3) {
                            String date = information.get(i).toString();
                            String amount = String.valueOf(information.get(i + 1));
                            String description = (String) information.get(i + 2);
                            table.addCell(dateCell(date));
                            table.addCell(amountCell(amount));
                            table.addCell(descriptionCell(description));

                            BigDecimal temp = new BigDecimal(((Double) information.get(i + 1)).doubleValue());
                            total = total.add(temp);
                        }

                        d.add(table);

                        total = total.setScale(2, BigDecimal.ROUND_HALF_EVEN);
                        Font bottomFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
                        p = new Paragraph("Total Contribution: " + total, bottomFont);
                        p.setAlignment(Element.ALIGN_CENTER);
                        p.setSpacingBefore(15f);
                        d.add(p);


                    } catch (DocumentException ex) {
                        Logger.getLogger(MonthlyReport.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
            
            d.close();

            File file = new File(filename);
            Desktop.getDesktop().open(file);
        } catch (DocumentException ex) {
            Logger.getLogger(TaxContributionMember.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(MonthlyReport.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static PdfPCell dateCell(String s) {
        Font f = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL);
        PdfPCell cell = new PdfPCell(new Paragraph(s, f));
        cell.setBorder(0);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        return cell;
    }

    private static PdfPCell amountCell(String s) {
        Font f = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL);
        PdfPCell cell = new PdfPCell(new Paragraph(s, f));
        cell.setBorder(0);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        return cell;
    }

    private static PdfPCell descriptionCell(String s) {
        Font f = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL);
        PdfPCell cell = new PdfPCell(new Paragraph(s, f));
        cell.setBorder(0);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        return cell;
    }

    private static PdfPCell headingCell(String s) {
        Font f = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL);
        PdfPCell cell = new PdfPCell(new Paragraph(s, f));
        cell.setBorder(0);
        cell.setBorderWidthBottom(0.5f);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        return cell;
    }
}
